Below is a simple function that takes one string argument and returns the word count in that string. In this function, we will use the following VB.NET built-in function:
- UBound: a function that returns the highest subscript for the indicated dimension of an array.
- Split: a function that splits strings. It extracts the substrings from the string that are delimited by a delimiter.
- Trim: a function that takes a string and returns it without leading or trailing spaces, if any.
- Replace: a function that replaces a specified part of a string with another string a determined number of times.
VB.NET
Function wordCount(ByVal str As String) Dim NumberOfWordAs Integer result = UBound(Split(Trim(Replace(str, Space(2), Space(1))))) + 1 Return NumberOfWord End Function
Now call this function from a button click event:
VB.NET
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MessageBox.Show("Total Word Count is " & countwords("Get words count in a string using VB.NET")) End Sub
And the result:
Comments
thanks for giving the useful information